package com.bdqn.systems.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.response.Result;
import com.bdqn.response.ResultCode;
import com.bdqn.systems.entity.User;
import com.bdqn.systems.service.UserService;
import com.bdqn.systems.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cp
 * @since 2020-11-30
 */
@Api(value = "用户信息接口",tags = "访问用户信息接口")
@CrossOrigin
@RestController
@RequestMapping("/systems/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "访问用户信息", notes = "访问所有用户信息")
    @PostMapping("/findUserList")
    public Result findUserList(@RequestParam(required = true,defaultValue = "1")Integer current,
                               @RequestParam(required = true,defaultValue = "5")Integer size,
                               @RequestBody UserVo userVo) {
        Page<User> page = new Page<>(current,size);
        Page<User> userList = userService.getUserList(page, userVo);
        return Result.ok().data("userList", userList);
    }

    @ApiOperation(value = "逻辑删除", notes = "删除用户信息")
    @DeleteMapping("/del")
    public Result del(Long id){
        Integer del = userService.del(id);
        return del>0?Result.ok():Result.error();
    }

    @ApiOperation(value = "添加用户信息接口", notes = "添加用户信息")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
        Integer insert = userService.insert(user);
        return insert>0?Result.ok():Result.error();
    }

    @ApiOperation(value = "修改用户信息接口", notes = "修改用户信息")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        boolean b = userService.updateById(user);
        return b?Result.ok():Result.error();
    }

    @ApiOperation(value = "编辑用户信息接口", notes = "获取单个用户信息")
    @GetMapping("/compileUser")
    public Result compileUser(Integer id){
        User user = userService.getBaseMapper().selectById(id);
        return Result.ok().data("user",user);
    }

    @ApiOperation(value = "编辑用户状态接口", notes = "禁用或解封用户状态")
    @GetMapping("/editUserState")
    public Result editUserState(Integer id){
        boolean b = userService.editUserState(id);
        return b?Result.ok():Result.error();
    }

    @ApiOperation(value = "导出用户信息",notes = "根据要求导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response,
                    @RequestBody(required = false)UserVo userVo) throws IOException{
        List<User> userList = userService.findUserList(userVo);
        userService.exportUserExcel(response,userList);
    }

}

